
<template>
  <div>
    <el-dialog v-model="centerDialogVisible" title="确认" width="30%" center>
      <template #default>您将删除条目:{{ fromDelete.name }}</template>
      <template #footer>
      <span class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取消</el-button>
        <el-button type="danger" :disabled="BtnConfirmAble" @click="deleteRow">确定</el-button>
      </span>
      </template>
    </el-dialog>

    <!--修改弹窗-->
    <el-dialog v-model="editDialogVisible" :title=formEdit.PopupType width="40%" center>
      <el-form :model="formEdit">
        <el-form-item label="图书类别名称" :label-width="formLabelWidth"
                      :rules="{required:!formEdit.typeName,message:'此项必填!'}">
          <el-input :size="inputSize" v-model="formEdit.typeName" autocomplete="off"/>
        </el-form-item>
      </el-form>
      <template #footer>
      <span class="dialog-footer">
        <el-button @click="Cancel">取消</el-button>
        <el-button type="primary" :disabled="BtnConfirmAble" @click="ConfirmEdit(formEdit)">确认</el-button>
      </span>
      </template>
    </el-dialog>
    <!--table显示-->
    <div style="margin-bottom: 10px;width: 100%">
      <el-button type="primary" size="default" @click="addInitialize">添加</el-button>
      <!--      <el-input
                style="margin-left: 60px;width: 250px"
                align="right"
                v-model.trim="search.bookName"
                class="w-50 m-2"
                size="default"
                @keyup.enter="getTableData"
                placeholder="模糊书名">
            </el-input>
            <el-button style="margin-left: 60px;" size="default" type="primary" @click="getTableData">
              <el-icon>
                <Search/>
              </el-icon>
            </el-button>-->
    </div>


    <el-scrollbar>
      <el-table v-loading="loading" :header-cell-style="{background:'#eef1f6',color:'#606266'}" border
                :row-style="{height: '54.7px'}"
                style="font-size: 15px" stripe :data="tableData">
        <!--        <el-table-column prop="tid" label="图书类型编号" width="100"/>-->
        <el-table-column prop="typeName" label="类型名称" width="100"/>
        <el-table-column fixed="right" label="操作">
          <template #default="scopeEdit">
            <el-button
                link
                type="success"
                size="small"
                @click.prevent="editRow(scopeEdit.row)">
              <el-icon>
                <Edit/>
              </el-icon>
              <span>编辑</span>
            </el-button>
            <el-button
                link
                type="danger"
                size="small"
                @click.prevent="deleteLog(scopeEdit.row.tid,
                ' { '+scopeEdit.row.typeName+' }')">
              <el-icon>
                <Delete/>
              </el-icon>
              <span>删除</span>
            </el-button>
          </template>
        </el-table-column>

      </el-table>
    </el-scrollbar>

  </div>
</template>

<script lang="ts" setup>

import {onMounted, reactive, ref, watch} from "vue";
import {Delete, Edit, Operation, Search} from '@element-plus/icons-vue'

import {
  deleteApi,
  getOneByIdApi,
  searchTypesApi, addApi, editTypeApi
} from "../api/types";
import {ElMessage} from "element-plus";
import {ElLoading} from "element-plus/es";

const centerDialogVisible = ref(false)

//搜索功能
const inputSearch = ref([])


//数据初始化
onMounted(() => {
  getTableData()
})

const rolesList = ref<any[]>([])
//分页
const page = reactive({
  current: 1,
  limit: 10,
  total: 0,
})


//分页操作
function handleCurrentChange(e: any) {
  getTableData()
}

const tableData = ref([])
const loading = ref(true)
const search = reactive({
  author: "",
  bookName: "",
  tid: ""
})

function getTableData() {
  searchTypesApi().then((response: any) => {
    if (response.code === 200) {
      tableData.value = response.data.typelist
      page.total = response.data.total
    } else {
      ElMessage.warning(response.message)
    }
    loading.value = false
  }).catch(function (res) {
    loading.value = false
    ElMessage.error("页面加载失败")
  })
}


//添加修改数据初始化
const BtnConfirmAble = ref(true)


const formEdit = reactive({
  tid: "",
  typeName: "",
  PopupType: ""
})


//弹窗展示编辑表格
function editRow(row: any) {
  formEdit.tid = row.tid
  formEdit.typeName = row.typeName
  formEdit.PopupType = PopupType.value[1]
  editDialogVisible.value = true;
}

//修改字段检测
watch(() => ({...formEdit}), (count, prevCount) => {
  if (count.typeName == '') {
    BtnConfirmAble.value = true
  } else {
    BtnConfirmAble.value = false
  }
})

function ConfirmEdit(e: any) {
  const loading = ElLoading.service({
    lock: true,
    text: 'Loading',
    background: 'rgba(0,0,0,0.7)',
  })
  BtnConfirmAble.value = true
  if (e.PopupType == PopupType.value[0]) {
    addApi(e)
        .then(function (response: any) {
          if (response.code === 200) {
            getTableData()
            editDialogVisible.value = false;
            ElMessage.success(response.message)
          } else {
            ElMessage.warning(response.message)
          }
          loading.close()
        }).catch(response => {
      ElMessage.error("修改失败")
      loading.close()
    })
  } else {

    editTypeApi(e)
        .then(function (response: any) {
          if (response.code === 200) {
            getTableData()
            editDialogVisible.value = false;
            ElMessage.success(response.message)
          } else {
            ElMessage.warning(response.message)
          }
          loading.close()
        }).catch(response => {
      ElMessage.error("修改失败")
      loading.close()
    })
  }

}

function Cancel() {
  editDialogVisible.value = false;
}


//添加系统用户
const editDialogVisible = ref(false)

const PopupType = ref(['添加', '编辑'])

function addInitialize() {
  formEdit.tid = "";
  formEdit.typeName = "";
  formEdit.PopupType = PopupType.value[0]
  editDialogVisible.value = true;
}

const fromDelete = reactive({
  id: -1,
  name: ''
})

//删除
function deleteLog(id: any, name: any) {
  fromDelete.id = id
  fromDelete.name = name
  centerDialogVisible.value = true;
  BtnConfirmAble.value = false;
}

function deleteRow() {
  const loading = ElLoading.service({
    lock: true,
    text: 'Loading',
    background: 'rgba(0,0,0,0.7)',
  })
  BtnConfirmAble.value = true
  deleteApi(fromDelete.id)
      .then(function (response: any) {
        if (response.code === 200) {
          getTableData()
          centerDialogVisible.value = false
          ElMessage.success("删除成功")
        }else {
          ElMessage.error(response.message)
        }
        loading.close()
      }).catch(response => {
    ElMessage.error("删除失败")
    loading.close()
  })
}

const formLabelWidth = '140px'
const inputSize = 'default'

</script>
